package cn.tuling.example;

/**
 * 客厅装修
 * 产品：客厅，包括墙体、地板、电视机、沙发等部件
 * 抽象建造者：工人
 * 具体建造者：工人1、工人2
 * 指挥者：项目经理（包工头）
 */
public class BuilderTest {
    public static void main(String[] args) {
        Decorator decoratorA = new ConcreteDecoratorA();
        ProjectManager projectManagerA = new ProductManagerA(decoratorA);
        Parlour parlourA = projectManagerA.constructParlour();
        System.out.println(parlourA.show());

        System.out.println("换了个项目经理和装修设计师");

        Decorator decoratorB = new ConcreteDecoratorB();
        ProjectManager projectManagerB = new ProductManagerB(decoratorB);
        Parlour parlourB = projectManagerB.constructParlour();
        System.out.println(parlourB.show());
    }
}

class Parlour {
    private String wall;
    private String tv;
    private String sofa;
    private String floor;

    public void setWall(String wall) {
        this.wall = wall;
    }

    public void setTv(String tv) {
        this.tv = tv;
    }

    public void setSofa(String sofa) {
        this.sofa = sofa;
    }

    public void setFloor(String floor) {
        this.floor = floor;
    }

    public String show() {
        return "Parlour{" +
                "wall='" + wall + '\'' +
                ", tv='" + tv + '\'' +
                ", sofa='" + sofa + '\'' +
                ", floor='" + floor + '\'' +
                '}';
    }
}

abstract class Decorator {
    protected Parlour parlour = new Parlour();

    protected abstract void buildWall();

    protected abstract void buildTv();

    protected abstract void buildSofa();

    protected abstract void buildFloor();

    protected Parlour getResult() {
        return parlour;
    }
}

class ConcreteDecoratorA extends Decorator {

    @Override
    protected void buildWall() {
        parlour.setWall("欧美沙发");
    }

    @Override
    protected void buildTv() {
        parlour.setTv("欧美电视");
    }

    @Override
    protected void buildSofa() {
        parlour.setSofa("欧美沙发");
    }

    @Override
    protected void buildFloor() {
        parlour.setFloor("欧美地板");
    }
}

class ConcreteDecoratorB extends Decorator {

    @Override
    protected void buildWall() {
        parlour.setWall("古风墙体");
    }

    @Override
    protected void buildTv() {
        parlour.setTv("古风电视");
    }

    @Override
    protected void buildSofa() {
        parlour.setSofa("古风沙发");
    }

    @Override
    protected void buildFloor() {
        parlour.setFloor("古风地板");
    }
}

/**
 * 建造顺序不一样
 */
abstract class ProjectManager {
    protected Decorator decorator;

    abstract Parlour constructParlour();
}

class ProductManagerA extends ProjectManager {
    public ProductManagerA(Decorator decorator) {
        this.decorator = decorator;
    }

    @Override
    public Parlour constructParlour() {
        decorator.buildFloor();
        decorator.buildTv();
        decorator.buildWall();
        decorator.buildSofa();
        return decorator.getResult();
    }
}

class ProductManagerB extends ProjectManager {
    public ProductManagerB(Decorator decorator) {
        this.decorator = decorator;
    }

    @Override
    public Parlour constructParlour() {
        decorator.buildTv();
        decorator.buildWall();
        decorator.buildFloor();
        decorator.buildSofa();
        return decorator.getResult();
    }
}